﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.CApCableStatusBarMessageUpdater
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System;
using System.ComponentModel;


namespace DnProcessSimulateCommands;

public class CApCableStatusBarMessageUpdater : IDisposable
{
  private CApCableStatusMessageType m_errorMessageStatusBeforeSimulationRunning;
  private string m_errorMessageBeforeSimulationRunning;
  private readonly ICApFLEXCableAttributeViewModel m_attributeViewModel;
  private readonly ICApFLEXSimulationPlayer m_simulationPlayer;
  private readonly ICApCableStatusMessageFactory m_statusMessageFactory;

  public CApCableStatusBarMessageUpdater(
    ICApFLEXCableAttributeViewModel attributeViewModel,
    ICApFLEXSimulationPlayer simulationPlayer,
    ICApCableStatusMessageFactory statusMessageFactory)
  {
    this.m_attributeViewModel = attributeViewModel ?? throw new ArgumentNullException(nameof (attributeViewModel));
    this.m_simulationPlayer = simulationPlayer ?? throw new ArgumentNullException(nameof (simulationPlayer));
    this.m_statusMessageFactory = statusMessageFactory ?? throw new ArgumentNullException(nameof (statusMessageFactory));
    this.m_errorMessageStatusBeforeSimulationRunning = this.m_attributeViewModel.StatusMessageType;
    this.m_errorMessageBeforeSimulationRunning = this.m_attributeViewModel.Message;
    this.m_attributeViewModel.PropertyChanging += new PropertyChangedEventHandler(this.AttributeViewModel_PropertyChanging);
    this.m_attributeViewModel.PropertyChanged += new PropertyChangedEventHandler(this.AttributeViewModel_PropertyChanged);
    this.m_attributeViewModel.PropertyAttributeChanged += new PropertyChangedEventHandler(this.AttributeViewModel_PropertyAttributeChanged);
    this.m_simulationPlayer.SimulationStarted += new CApCableEventHandler(this.SimulationPlayer_SimulationStarted);
    this.m_simulationPlayer.SimulationStopped += new CApCableEventHandler(this.SimulationPlayer_SimulationStopped);
    this.UpdateStatusBarMessage();
  }

  public void Dispose()
  {
    this.m_attributeViewModel.PropertyChanging -= new PropertyChangedEventHandler(this.AttributeViewModel_PropertyChanging);
    this.m_attributeViewModel.PropertyChanged -= new PropertyChangedEventHandler(this.AttributeViewModel_PropertyChanged);
    this.m_attributeViewModel.PropertyAttributeChanged -= new PropertyChangedEventHandler(this.AttributeViewModel_PropertyAttributeChanged);
    this.m_simulationPlayer.SimulationStarted -= new CApCableEventHandler(this.SimulationPlayer_SimulationStarted);
    this.m_simulationPlayer.SimulationStopped -= new CApCableEventHandler(this.SimulationPlayer_SimulationStopped);
  }

  private void UpdateStatusBarMessage()
  {
    if (this.m_simulationPlayer.IsSimulationRunning())
    {
      ICApCableStatusMessage lengthErrorMessage = this.m_statusMessageFactory.ComputeCableLengthErrorMessage();
      if (lengthErrorMessage.MessageType == CApCableStatusMessageType.Error)
      {
        this.m_attributeViewModel.Message = lengthErrorMessage.MessageText;
        this.m_attributeViewModel.StatusMessageType = lengthErrorMessage.MessageType;
      }
      else
      {
        if (this.m_errorMessageStatusBeforeSimulationRunning == CApCableStatusMessageType.None)
          return;
        this.m_attributeViewModel.Message = this.m_errorMessageBeforeSimulationRunning;
        this.m_attributeViewModel.StatusMessageType = this.m_errorMessageStatusBeforeSimulationRunning;
      }
    }
    else
    {
      this.m_attributeViewModel.Message = string.Empty;
      this.m_attributeViewModel.StatusMessageType = CApCableStatusMessageType.None;
      ICApCableStatusMessage priorityErrorMessage = this.m_statusMessageFactory.ComputePriorityErrorMessage();
      this.m_attributeViewModel.Message = priorityErrorMessage.MessageText;
      this.m_attributeViewModel.StatusMessageType = priorityErrorMessage.MessageType;
    }
  }

  private void OnPropertyChange(string propertyName)
  {
    if (!(propertyName == "CreationMode") && !(propertyName == "Cable") && !this.m_attributeViewModel.IsCreationParameter(propertyName) && !this.m_attributeViewModel.IsFrameParameter(propertyName) && !this.m_attributeViewModel.IsOptimizationParameter(propertyName))
      return;
    this.UpdateStatusBarMessage();
  }

  private void AttributeViewModel_PropertyChanging(object sender, PropertyChangedEventArgs e)
  {
    this.OnPropertyChange(e.PropertyName);
  }

  private void AttributeViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
  {
    this.OnPropertyChange(e.PropertyName);
  }

  private void AttributeViewModel_PropertyAttributeChanged(
    object sender,
    PropertyChangedEventArgs e)
  {
    this.OnPropertyChange(e.PropertyName);
  }

  private void SimulationPlayer_SimulationStarted()
  {
    this.m_errorMessageStatusBeforeSimulationRunning = this.m_attributeViewModel.StatusMessageType;
    this.m_errorMessageBeforeSimulationRunning = this.m_attributeViewModel.Message;
  }

  private void SimulationPlayer_SimulationStopped() => this.UpdateStatusBarMessage();
}
